package com.fit.dao.homework;

import java.util.List;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.homework.Homework;
import com.fit.entities.user.User;

public class HomeworkDAOImpl implements HomeworkDAO {

	@Inject
	private Session session;

	@Log
	public boolean save(Homework newHomework) {
		if (newHomework != null) {
			session.save(newHomework);
			return true;
		}
		return false;
	}

	@Log
	public boolean delete(Homework homework) {
		if (homework != null) {
			session.delete(homework);
			return true;
		}
		return false;
	}

	@Log
	public boolean update(Homework homework) {
		if (homework != null) {
			session.update(homework);
			return true;
		}
		return false;
	}

	@Log
	public List findAll() {
		return session.createCriteria(Homework.class).list();
	}

	@Log
	public boolean deleteByID(Long id) {
		Homework u = findByID(id);
		if (u != null) {
			delete(u);
			return true;
		}
		return false;
	}

	@Log
	public Homework findByID(Long id) {
		if (id != null) {
			return (Homework) session.get(Homework.class, id);
		}
		return null;
	}

	@Log
	public Homework findByTask(String task) {
		if (task == null ) {
			return null;
		}

		if (task.equals("") ) {
			return null;
		}

		List l = session.createCriteria(Homework.class)
				.add(Restrictions.eq("task", task)).list();
		Homework u = null;
		if (l != null) {
			if (l.size() > 0) {
				u = (Homework) l.get(0);
			}
		}
		return u;
	}

}
